package com.fishleap.speaker;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * @author zlg
 */
public class SpeakReducer extends Reducer<Text, SpeakBean, Text, SpeakBean> {

    @Override
    protected void reduce(Text key, Iterable<SpeakBean> values, Context context) throws IOException, InterruptedException {
        long self_duration_sum = 0;
        long thirdPart_duration_sum = 0;

        // 1.遍历所用bean，将其中的自有，第三方时长分别累加
        for (SpeakBean value : values) {
            self_duration_sum += value.getSelfDuration();
            thirdPart_duration_sum += value.getThirdPartDuration();
        }
        // 2.封装对象
        SpeakBean outValue = new SpeakBean(self_duration_sum, thirdPart_duration_sum);
        // 3.写出
        context.write(key, outValue);
    }
}
